// pagesCollect/pages/collect/edit/speciality/speciality.js
import { getGoodsMsg, updateGoodsMsg, getGoodsClassify } from "../../../../../api/collect"

const app = getApp()

const rules = [
  { key: 'goodsName', msg: '请输入特产名称' },
  // { key: 'classifyId', msg: '请选择特产类型' },
  // { key: 'manufacturer', msg: '请输入生产厂家' },
  // { key: 'price', msg: '请输入价格' },
  { key: 'introduce', msg: '请选输入特产简介' },
  { key: 'goodsImage', msg: '请上传特产图片' },
  { key: 'address', msg: '请上特产地址' },
  { key: 'longitude', msg: '请选择特产定位' }]

Page({
  data: {
    navData: {
      title: '特产数据完善',
      level: 10
    },
    id: '',
    form: {},
    navHeight: 32,
    navTop: 48,
    imageStr: '',
    indexobj: {
      classifyId: 0,
    },
    goodsClassify: [],
    position: {},
  },
  onLoad(e) {
    this.getGoodsClassify()

    this.setData({
      navHeight: app.globalData.navHeight, //导航栏高度
      navTop: app.globalData.navTop, //导航栏距顶部距离
    });
    this.data.form.villageId = e.villageId;
    if (e.id) {
      this.data.id = e.id;
      this.getVillage()
    }
  },
  getGoodsClassify() {
    getGoodsClassify().then(res => {
      this.setData({
        goodsClassify: res.data.data
      })
    })
  },
  getVillage() {
    getGoodsMsg({ id: this.data.id }).then(res => {
      console.log(res)
      this.setData({
        ['navData.title']: res.data.data.goodsName + '数据完善',
        form: res.data.data,
        imageStr: res.data.data.goodsImage,
        position: {
          longitude: res.data.data.longitude,
          latitude: res.data.data.latitude
        },
      })
    })
  },
  submit() {
    let sendData = JSON.parse(JSON.stringify(this.data.form));
    let errMsg = null;
    for (let i = 0; i < rules.length; i++) {
      let key = rules[i].key;
      if (!sendData[key]) {
        errMsg = rules[i].msg;
        break;
      }
    }
    if (errMsg) {
      wx.showToast({
        icon: 'none',
        title: errMsg,
      })
      return false;
    }
    updateGoodsMsg(sendData).then(res => {
      let pages = getCurrentPages();
      let prefrctPage = pages[pages.length - 2];
      prefrctPage.refresh()
      let listPage = pages[pages.length - 3];
      listPage.refresh()
      wx.showModal({
        content: res.data.msg,
        showCancel: false,
        complete: (res) => {
          if (res.confirm) {
            wx.navigateBack()
          }
        }
      })
    })
  },
  toEdit() {
    app.globalData.introduceStr = this.data.form.introduce;
    wx.navigateTo({
      url: '/pagesCollect/pages/collect/edit/editor/editor?key=introduce',
    })
  },
  goodsClassChg(e) {
    let key = e.currentTarget.dataset.key,
      range = e.currentTarget.dataset.range,
      value = e.detail.value;
    this.setData({
      ['indexObj.classifyId']: value,
      ['form.classifyId']: this.data.goodsClassify[value].classifyId
    })
  },
  intChg(e) {
    let key = e.currentTarget.dataset.key,
      value = e.detail.value;
    this.setData({
      ['form.' + key]: value
    })
  },
  dwChg(e) {
    let {
      longitude,
      latitude
    } = e.detail
    this.setData({
      ['form.longitude']: longitude,
      ['form.latitude']: latitude
    })
  },
  addressChg(e){
    this.setData({
      ['form.address']: e.detail,
    })
  },
  imageChg(e) {
    let imgStr = e.detail;
    let imgArr = imgStr.split(',')
    let imgScaleArr = imgArr.map(img => {
      let res = img.split('.');
      return res[0] + '-scale.' + res[1]
    })
    this.setData({
      ['form.goodsImage']: imgStr,
      ['form.imageScale']: imgScaleArr.join(','),
    })
  },
})